//
//  FlickViewController.m
//  iOS UI Sample Code
//
//  Created by Kyusung Lee on 12. 2. 28..
//  Copyright (c) 2012년 kkamci77@gmail.com. All rights reserved.
//

#import "FlickViewController.h"

@interface FlickViewController ()

@end

@implementation FlickViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch * touch = [touches anyObject];
    timestampBegan_ = event.timestamp;
    pointBegan_ = [touch locationInView:self.view];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    static const NSTimeInterval kFlickJudgeTimeInterval = 0.3;
    static const NSInteger kFlickMinimumDistance = 10;
    UITouch * touchEnded = [touches anyObject];
    CGPoint pointEnded = [touchEnded locationInView:self.view];
    NSInteger distanceHorizontal = ABS(pointEnded.x - pointBegan_.x);
    NSInteger distanceVertical = ABS(pointEnded.y - pointBegan_.y);
    
    if(kFlickMinimumDistance > distanceHorizontal && kFlickMinimumDistance > distanceVertical)
    {
        return;
    }
    
    NSTimeInterval timeBeganToEnded = event.timestamp - timestampBegan_;
    if(kFlickJudgeTimeInterval > timeBeganToEnded)
    {
        NSString * message;
        
        if(distanceHorizontal > distanceVertical)
        {
            if(pointEnded.x > pointBegan_.x)
            {
                message = @"오른쪽 플릭 검출 !";
            }
            else {
                message = @"왼쪽 플릭 검출 !";
            }
        }else {
            if(pointEnded.y > pointBegan_.y)
            {
                message = @"아래 플릭 검출 !";
            }else {
                message = @"위 플릭 검출 !";
            }
        }
        
        UIAlertView * alert = [[[UIAlertView alloc] initWithTitle:nil message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil] autorelease];
        [alert show];
    }
}

@end
